package com.qigao.mall.web.controller.admin.merchant;

import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.BonusDetailService;
import com.qigao.mall.api.SettleTotalService;
import com.qigao.mall.commons.dto.*;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.utils.excel.ExcelFormat;
import com.qigao.mall.commons.utils.excel.ExcelHeaderInfo;
import com.qigao.mall.commons.utils.excel.ExcelUtils;
import com.qigao.mall.web.controller.AbstractExcelController;
import com.qigao.mall.web.vo.admin.merchant.BonusCondVo;
import com.qigao.mall.web.vo.admin.merchant.ProfitCondVo;
import com.qigao.mall.web.vo.admin.merchant.SettleCondVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/bonus/")
@Api(tags = "Admin商户操作分红相关API" ,value = "Admin商户操作分红相关API")
public class AdminBonusController extends AbstractExcelController {

    private static Logger logger= LoggerFactory.getLogger(AdminBonusController.class);
    @Resource
    private BonusDetailService  bonusDetailService;

    @PostMapping("/listPage")
    @ApiOperation(value = "按照分红状态查询列表", notes = "按照分红状态查询列表")
    public Result<PageInfo<BonusDetailResultDto>> listPage(@RequestBody BonusCondVo bonusCondVo, HttpServletRequest request){
        try{
            //参数校验
            BonusCondDto bonusCondDto=new BonusCondDto();
            BeanUtils.copyProperties(bonusCondVo,bonusCondDto);
            if(StringUtils.isNotEmpty(bonusCondVo.getDateBegin())){
                bonusCondDto.setDateBegin(bonusCondVo.getDateBegin()+" 00:00:00");
            }
            if(StringUtils.isNotEmpty(bonusCondVo.getDateEnd())){
                bonusCondDto.setDateEnd(bonusCondVo.getDateEnd()+" 23:59:59");
            }
            return  bonusDetailService.listPage(bonusCondDto);
        }catch (Exception e){
            logger.error("[按照分红状态查询列表]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    @GetMapping(value = "/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void exportGet(BonusCondVo bonusCondVo, HttpServletResponse response) {
        BonusCondDto bonusCondDto=new BonusCondDto();
        BeanUtils.copyProperties(bonusCondVo,bonusCondDto);
        if(StringUtils.isNotEmpty(bonusCondVo.getDateBegin())){
            bonusCondDto.setDateBegin(bonusCondVo.getDateBegin()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(bonusCondVo.getDateEnd())){
            bonusCondDto.setDateEnd(bonusCondVo.getDateEnd()+" 23:59:59");
        }
        // 待导出数据
        List<BonusDetailResultDto> orderResultDtos = bonusDetailService.findOrderResultByCond(bonusCondDto);
        ExcelUtils excelUtils = new ExcelUtils(orderResultDtos, getHeaderInfo(), getFormatInfo());
        sendHttpResponse(response, "bonusReport", excelUtils.getWorkbook());
    }
    @PostMapping(value = "/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void export(@RequestBody  BonusCondVo bonusCondVo, HttpServletResponse response) {
        BonusCondDto bonusCondDto=new BonusCondDto();
        BeanUtils.copyProperties(bonusCondVo,bonusCondDto);
        if(StringUtils.isNotEmpty(bonusCondVo.getDateBegin())){
            bonusCondDto.setDateBegin(bonusCondVo.getDateBegin()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(bonusCondVo.getDateEnd())){
            bonusCondDto.setDateEnd(bonusCondVo.getDateEnd()+" 23:59:59");
        }
        // 待导出数据
        List<BonusDetailResultDto> orderResultDtos = bonusDetailService.findOrderResultByCond(bonusCondDto);
        ExcelUtils excelUtils = new ExcelUtils(orderResultDtos, getHeaderInfo(), getFormatInfo());
        sendHttpResponse(response, "bonusReport", excelUtils.getWorkbook());
    }

    // 获取表头信息
    private List<ExcelHeaderInfo> getHeaderInfo() {
        return Arrays.asList(
                new ExcelHeaderInfo(0, 0, 0, 0, "结算日期"),
                new ExcelHeaderInfo(0, 0, 1, 1, "姓名"),
                new ExcelHeaderInfo(0, 0, 2, 2, "用户级别"),
                new ExcelHeaderInfo(0, 0, 3, 3, "级别数"),
                new ExcelHeaderInfo(0, 0, 4, 4, "总级别数"),
                new ExcelHeaderInfo(0, 0, 5, 5, "订单数"),
                new ExcelHeaderInfo(0, 0, 6, 6, "总订单数"),
                new ExcelHeaderInfo(0, 0, 7, 7, "所属公司"),
                new ExcelHeaderInfo(0, 0, 8,8, "公司当期利润"),
                new ExcelHeaderInfo(0, 0, 9, 9, "销售应得分红"),
                new ExcelHeaderInfo(0, 0, 10,10, "本级别应得分红"),
                new ExcelHeaderInfo(0, 0, 11,11, "当期应得总分红")
        );
    }

    // 获取格式化信息
    private Map<String, ExcelFormat> getFormatInfo() {
        Map<String, ExcelFormat> format = new HashMap<>();
       /* format.put("id", ExcelFormat.FORMAT_INTEGER);
        format.put("categoryId", ExcelFormat.FORMAT_INTEGER);
    */
        return null;
    }





}
